/*
 * Copyright (c) 2001, Aslak Hellesøy, BEKK Consulting
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * - Neither the name of BEKK Consulting nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */
package middlegen.plugins.entitybean;

import middlegen.javax.JavaPlugin;
import middlegen.FileProducer;
import middlegen.Util;
import middlegen.plugins.entitybean.swing.JEntity30RelationSettingsPanel;
import middlegen.plugins.entitybean.swing.JEntity30TableSettingsPanel;
import middlegen.plugins.entitybean.swing.JEntityColumnSettingsPanel;
import middlegen.swing.JRelationSettingsPanel;
import middlegen.swing.JTableSettingsPanel;
import middlegen.swing.JColumnSettingsPanel;

/**
 * This plugin generates Enterprise Java Beans of type EntityBean conforming to the EJB 3.0 specification, using
 * Container-managed persistence (CMP).
 *
 * @author <a href="mailto:eivind.waaler@bekk.no">Eivind Waaler</a>
 * @created 12. oktober 2004
 * @version $Id: CMP30Plugin.java,v 1.6 2005/11/15 19:36:49 eivindw Exp $
 * @middlegen.plugin
 *       name = "cmp30"
 *
 */
public class CMP30Plugin extends JavaPlugin {

   /**
    * @todo-javadoc Describe the field
    */
   private boolean _includeConnectionAnnotation = false;

   /** Standard constructor. */
   public CMP30Plugin() {
      super();
      setDisplayName("CMP 3.0");
   }

   /**
    * Sets the IncludeConnectionAnnotation attribute of the CMP30Plugin object.
    *
    * @param includeConnectionAnnotation The new IncludeConnectionAnnotation value.
    */
   public void setIncludeConnectionAnnotation(boolean includeConnectionAnnotation) {
      _includeConnectionAnnotation = includeConnectionAnnotation;
   }

   /**
    * Gets the TableDecoratorClass attribute of the CMP30Plugin object.
    *
    * @return The TableDecoratorClass value.
    */
   public Class getTableDecoratorClass() {
      return CMP30Table.class;
   }

   /**
    * Gets the ColumnDecoratorClass attribute of the CMP30Plugin object.
    *
    * @return The ColumnDecoratorClass value.
    */
   public Class getColumnDecoratorClass() {
      return CMP30Column.class;
   }

   /**
    * {@inheritDoc}
    *
    * @return The RelationSettingsPanel value.
    */
   public JRelationSettingsPanel getRelationSettingsPanel() {
      return new JEntity30RelationSettingsPanel();
   }

   /**
    * {@inheritDoc}
    *
    * @return The TableSettingsPanel value.
    */
   public JTableSettingsPanel getTableSettingsPanel() {
      return new JEntity30TableSettingsPanel();
   }

   /**
    * {@inheritDoc}
    *
    * @return The ColumnSettingsPanel value.
    */
   public JColumnSettingsPanel getColumnSettingsPanel() {
      return new JEntityColumnSettingsPanel();
   }

   /**
    * Gets the ConnectionAnnotation attribute of the CMP30Plugin object.
    *
    * @return The ConnectionAnnotation value.
    */
   public String getConnectionAnnotation() {
      String ret = "";
      if (_includeConnectionAnnotation) {
         String schema = Util.ensureNotNull(getMiddlegen().getMiddlegenTask().getSchema());
         String catalog = Util.ensureNotNull(getMiddlegen().getMiddlegenTask().getCatalog());
         if (schema.length() != 0) {
            ret += ", schema = \"" + schema + "\"";
         }
         if (catalog.length() != 0) {
            ret += ", catalog = \"" + catalog + "\"";
         }
      }
      return ret;
   }

   /**
    * Add the file producers. This class only produces one file per table, based on the entity-cmp-30.vm template.
    */
   protected void registerFileProducers() {
      super.registerFileProducers();
      addConfiguredFileproducer(new FileProducer(getDestinationDir(), "{0}.java", getClass().getResource("entity-cmp-30.vm")));
   }
}
